
//
//  TopicViewController.m
//  XWZ_News_Work
//
//  Created by penngshi on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TopicViewController.h"
#import "FindTableViewCell.h"
#import "FinfModel.h"
#import "TopicDetailViewController.h"
@interface TopicViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)UITableView *myTableView;

@property (nonatomic ,strong)NSMutableArray *allArray;

@property (nonatomic ,assign)NSInteger pageNumber;

///判断上拉还是下拉
@property (nonatomic ,assign)BOOL upLoad;
@end

@implementation TopicViewController


- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)loadView {
    [super loadView];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
#pragma mark - 初始化allArray
    self.allArray = [NSMutableArray array];
    
    self.navigationItem.title = @"问吧";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    
#pragma mark - 创建tableView
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 46) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
#pragma mark - 注册cell
    [self.myTableView registerClass:[FindTableViewCell class] forCellReuseIdentifier:@"cell"];
    //    //调用请求数据的方法
    //    [self getData];
    [self addHeaderReflash];
    [self.myTableView.mj_header beginRefreshing];
    [self addFooterReflash];
    
}

#pragma mark - 下拉刷新
- (void)addHeaderReflash {
    self.upLoad = YES;
    self.pageNumber = 0;
    [self.myTableView.mj_header beginRefreshing];
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    self.myTableView.mj_header = header;
    
}
#pragma mark - 下拉加载
- (void)addFooterReflash {
    
    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.upLoad = NO;
        self.pageNumber = self.pageNumber + 10;
        [self getData];
    }];
    
}

#pragma mark - 解析数据
- (void)getData {
    
    
    NSString *urlStr =[NSString stringWithFormat:@"http://c.3g.163.com/newstopic/list/expert/%ld-10.html",self.pageNumber];

    [xwzAFNetTool getNetWithURL:urlStr body:nil headFile:nil responStyle:XWZ_JSON success:^(NSURLSessionDataTask *task, id responseObject) {
        if (self.upLoad == YES) {
            
                        [self.allArray removeAllObjects];
                    }
            
                    NSMutableDictionary *bigDic = responseObject;
                    NSMutableDictionary *dataDic = [bigDic objectForKey:@"data"];
                    NSMutableArray *expertListArr = [dataDic objectForKey:@"expertList"];
                    for (NSDictionary *dic in expertListArr) {
            
                        FinfModel *model = [[FinfModel alloc] init];
                        [model setValuesForKeysWithDictionary:dic];
            
                        [self.allArray addObject:model];
                    }
                    
                    [self.myTableView.mj_header endRefreshing];
                    [self.myTableView.mj_footer endRefreshing];
                    [self.myTableView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
}




#pragma mark - 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    FinfModel *model = [self.allArray objectAtIndex:indexPath.row];
    
    cell.findModel = model;
    
    return cell;
    
}

#pragma mark - 必要的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.allArray.count;
}

#pragma mark - 高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 345 * SIZE;
}

#pragma mark - 点击方式
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //取消点击至灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TopicDetailViewController *findDetailVC = [[TopicDetailViewController alloc] init];
    FinfModel *model = [self.allArray objectAtIndex:indexPath.row];
    findDetailVC.expertId = model.expertId;
    findDetailVC.alias = model.alias;
    [self.navigationController pushViewController:findDetailVC animated:YES];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
